package com.lonwin.mes2u8.domain.u8;

import com.google.common.collect.Lists;
import com.lonwin.mes2u8.common.CommonValue;
import com.lonwin.mes2u8.domain.mes.MesMaterialOutQueryResult;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

/**
 * @author DELL
 * @Date 2023-12-18 15:03
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class U8MaterialOutAddParam {

    private U8MaterialOutAdd materialout;




    public static U8MaterialOutAddParam buildU8MaterialOutAddParamByMesQueryResult(List<MesMaterialOutQueryResult> resultList) {
        if (resultList.isEmpty()) {
            return null;
        }

        String code = "";

        List<U8MaterialOutAddEntry> entries = Lists.newArrayList();
        for (int i = 0 ; i < resultList.size(); i++) {
            MesMaterialOutQueryResult result = resultList.get(i);
            code = result.getSno();
            U8MaterialOutAddEntry addEntry = U8MaterialOutAddEntry.builder()
                    .inventorycode(result.getSpono().replace("-", ""))
                    // .quantity(result.getSumRealityBlanking().divide(new BigDecimal("1000"), 5, RoundingMode.HALF_EVEN))
                    .quantity(BigDecimal.ZERO)
                    .rowno(i+1)
                    .build();
            entries.add(addEntry);
        }
        return U8MaterialOutAddParam.builder()
                .materialout(U8MaterialOutAdd.builder()
                        .warehousecode("12").receivecode("205").define10(code)
                        .entry(entries).build())
                .build();
    }

    public static U8MaterialOutAddParam buildU8MaterialOutAddParamByBillHandlerResult(U8MaterialOutHandlerResult u8MaterialOutHandlerResult) {

        if (Objects.isNull(u8MaterialOutHandlerResult) || Objects.isNull(u8MaterialOutHandlerResult.getInfoList()) || u8MaterialOutHandlerResult.getInfoList().isEmpty()) {
            return null;
        }

        List<U8MaterialOutAddEntry> entries = Lists.newArrayList();
        for (int i = 0; i < u8MaterialOutHandlerResult.getInfoList().size(); i++) {
            U8MaterialOutHandlerResultInfo billInfo = u8MaterialOutHandlerResult.getInfoList().get(i);

            U8MaterialOutAddEntry addEntry = U8MaterialOutAddEntry.builder()
                    .inventorycode(billInfo.getMno())
                    .quantity(billInfo.getNumber())
                    .number(billInfo.getWeight())
                    .irate(billInfo.getWeight().divide(billInfo.getNumber(), 6, RoundingMode.HALF_UP))
                    .rowno(i+1)
                    .build();

            entries.add(addEntry);
        }

        return U8MaterialOutAddParam.builder()
                .materialout(U8MaterialOutAdd.builder()
                        .warehousecode(CommonValue.U8_MATERIAL_OUT_WARE_HOUSE).receivecode("205").memory(u8MaterialOutHandlerResult.getInventoryCode())
                        .maker(CommonValue.U8_MATERIAL_OUT_MAKER)
                        .entry(entries).build())
                .build();
    }
}
